//
//  LGTRWZTimeSelectVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/23.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import WisdomHUD

class LGTRWZCommenSelectVC: LGTBaseVC, UIPickerViewDataSource, UIPickerViewDelegate{
    var type:Int = 0 //0 是时间 1 是 次数
    let pickView:UIPickerView  = UIPickerView.init()
    var data:Array<PostNameModel> = Array.init()
    var path:IndexPath?
    var currentModel:PostNameModel?
    var clickBlock:ClickPostNameModelBlock?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.pickView.reloadAllComponents()
        
//        //跳转指定位置
//        if self.currentModel != nil{
//            for i in 0..<self.data.count{
//                let model:PostNameModel = self.data[i]
//                if model.value == model.value{
//                    self.pickView.selectRow(i, inComponent: 0, animated: false)
//                    break
//                }
//            }
//        }
    }
    
    override func setUI() {
        super.setUI()
        self.view.backgroundColor = BlackAlphaColor
        let backView:UIView = UIView.init()
        backView.backgroundColor = UIColor.white
        self.view.addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(SizeFit(210) + IPHONETabbar_DIS())
        }
        
        let topView:UIView = UIView.init()
        topView.backgroundColor = ColorUtil(color: 0xFFDFA0)
        backView.addSubview(topView)
        topView.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(0)
            make.height.equalTo(SizeFit(48))
        }
        
        let clostBtn = LGTViewCreateUtil.share.createTextLGTButton(title: "取消", font: Font_Normal_18, color: TextColor, selectedColor: TextColor, dW: SizeFitFloat(5), dH: SizeFitFloat(5))
        topView.addSubview(clostBtn)
        clostBtn.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(14))
            make.height.equalTo(SizeFitFloat(18))
            make.width.equalTo(SizeFit(40))
            make.centerY.equalTo(topView)
        }
        clostBtn.backgroundColor = UIColor.clear
        
        let sureBtn = LGTViewCreateUtil.share.createTextLGTButton(title: "确定", font: Font_Normal_18, color: YellowColor, selectedColor: YellowColor, dW: SizeFitFloat(5), dH: SizeFitFloat(5))
        topView.addSubview(sureBtn)
        sureBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-SizeFit(14))
            make.height.equalTo(SizeFitFloat(18))
            make.width.equalTo(SizeFit(40))
            make.centerY.equalTo(topView)
        }
        sureBtn.backgroundColor = UIColor.clear
        
        
        backView.addSubview(self.pickView)
        self.pickView.delegate = self as UIPickerViewDelegate
        self.pickView.dataSource = self
        self.pickView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalTo(0)
            make.top.equalTo(SizeFit(48))
        }
        
        clostBtn.addTarget(self, action: #selector(closeAction), for: UIControl.Event.touchUpInside)
        sureBtn.addTarget(self, action: #selector(sureAction), for: UIControl.Event.touchUpInside)
    }
    
    @objc func closeAction(){
        self.dismiss(animated: false, completion: nil)
    }
    
    @objc func sureAction(){
        if self.clickBlock != nil{
            let model:PostNameModel = self.data[self.pickView.selectedRow(inComponent: 0)]
            if model == nil{
                self.dismiss(animated: false, completion: nil)
                return
            }
            self.clickBlock!(model, self.path!)
        }
        self.dismiss(animated: false, completion: nil)
    }
    
    //高度
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat{
        return SizeFitFloat(49)
    }
    
    // 设置组的数量
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.data.count
    }
    
    // 设置设置每组的宽度
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
     return KScreenW
    }
    
    // 根据组下标返回一个视图
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let model = self.data[row]
        let text = model.name ?? ""
        let label:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(18), text: text)
        return label
    }
}
